package com.itlzh.mall.cart.Service.impl;

import com.itlzh.mall.cart.Service.ICartService;
import com.itlzh.mall.common.enums.ResponseEnum;
import com.itlzh.mall.proto.Cart;
import com.itlzh.mall.proto.OrderRpcSimpleServiceGrpc;
import com.itlzh.mall.proto.ProtoParam;
import com.itlzh.mall.proto.ProtoResult;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;
@GrpcService
public class GrepCartServiceImpl extends OrderRpcSimpleServiceGrpc.OrderRpcSimpleServiceImplBase {
    @Autowired
    private ICartService cartService;
    public void deleteCart(ProtoParam request, StreamObserver<ProtoResult> responseObserver){
        //获取请求参数
        List<Cart> cartsList = request.getCartsList();
        //提取参数id
        List<String> ids = cartsList.stream().map(Cart::getId).toList();
        //删除购物车
        try {
            //删除购物车
            cartService.removeByIds(ids);
            ProtoResult result = ProtoResult.newBuilder()
                    .setCode(ResponseEnum.SUCCESS.getCode())
                    .setMessage(ResponseEnum.SUCCESS.getMessage())
                    .build();
            responseObserver.onNext(result);
        } catch (Exception e) {
            ProtoResult result = ProtoResult.newBuilder()
                    .setCode(ResponseEnum.FAIL.getCode())
                    .setMessage(e.getMessage())
                    .build();
        } finally {
            //完成调用
            responseObserver.onCompleted();
        }
    }
}
